package Views;

import Controllers.MachineCTR;
import Models.Machine;
import Views.TableModels.MachineTableModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JOptionPane;

/**
 * @author Morten
 */
public class MachineView extends javax.swing.JFrame implements WindowListener
{
	private MachineCTR controller = new MachineCTR();

    public MachineView()
	{
        initComponents();
		findTable.addMouseListener(new MouseAdapter(){
			@Override
			public void mouseClicked(MouseEvent e){
				if (e.getClickCount() == 2)
				{
					if(findTable.getSelectedRowCount() == 1)
					{
						int chosenId = Integer.parseInt(findTable.getValueAt(findTable.getSelectedRow(), 0).toString());
						new MachineInfoView(controller.getMachine(chosenId)).setVisible(true);
					}
				}
			}
		 });
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        availableBG = new javax.swing.ButtonGroup();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        addMachinePanel = new javax.swing.JPanel();
        addDescriptionLabel = new javax.swing.JLabel();
        addNameTF = new javax.swing.JTextField();
        addNameLabel = new javax.swing.JLabel();
        addButton = new javax.swing.JButton();
        findMachinePanel = new javax.swing.JPanel();
        findDescriptionLabel = new javax.swing.JLabel();
        findNameLabel = new javax.swing.JLabel();
        findNameTF = new javax.swing.JTextField();
        availableAllRB = new javax.swing.JRadioButton();
        availableHomeRB = new javax.swing.JRadioButton();
        availableOutRB = new javax.swing.JRadioButton();
        jScrollPane = new javax.swing.JScrollPane();
        findTable = new javax.swing.JTable();
        findButton = new javax.swing.JButton();
        findShowAllButton = new javax.swing.JButton();
        getMachinePanel = new javax.swing.JPanel();
        getDescriptionLabel = new javax.swing.JLabel();
        getIdSP = new javax.swing.JSpinner();
        getButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Maskiner");

        addDescriptionLabel.setText("Her kan der tilføjes maskiner til udlejning");

        addNameLabel.setText("Navn:");

        addButton.setText("Opret");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout addMachinePanelLayout = new org.jdesktop.layout.GroupLayout(addMachinePanel);
        addMachinePanel.setLayout(addMachinePanelLayout);
        addMachinePanelLayout.setHorizontalGroup(
            addMachinePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(addMachinePanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(addMachinePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(addDescriptionLabel)
                    .add(addMachinePanelLayout.createSequentialGroup()
                        .add(addNameLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(addNameTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 109, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(addButton))
                .addContainerGap(77, Short.MAX_VALUE))
        );
        addMachinePanelLayout.setVerticalGroup(
            addMachinePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(addMachinePanelLayout.createSequentialGroup()
                .add(addDescriptionLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(addMachinePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(addNameLabel)
                    .add(addNameTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(addButton)
                .addContainerGap(194, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Tilføj", addMachinePanel);

        findDescriptionLabel.setText("Her kan der søges på navnet på maskiner");

        findNameLabel.setText("Navn:");

        availableBG.add(availableAllRB);
        availableAllRB.setSelected(true);
        availableAllRB.setText("*");

        availableBG.add(availableHomeRB);
        availableHomeRB.setText("Hjemme");

        availableBG.add(availableOutRB);
        availableOutRB.setText("Udlejet");

        findTable.setModel(new MachineTableModel());
        findTable.setColumnSelectionAllowed(true);
        findTable.getTableHeader().setReorderingAllowed(false);
        jScrollPane.setViewportView(findTable);
        findTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        findButton.setText("Søg");
        findButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findButtonActionPerformed(evt);
            }
        });

        findShowAllButton.setText("Vis alle");
        findShowAllButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findShowAllButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout findMachinePanelLayout = new org.jdesktop.layout.GroupLayout(findMachinePanel);
        findMachinePanel.setLayout(findMachinePanelLayout);
        findMachinePanelLayout.setHorizontalGroup(
            findMachinePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(findMachinePanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(findMachinePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE)
                    .add(findDescriptionLabel)
                    .add(findMachinePanelLayout.createSequentialGroup()
                        .add(availableAllRB)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(availableHomeRB)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(availableOutRB))
                    .add(findMachinePanelLayout.createSequentialGroup()
                        .add(findNameLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(findNameTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 170, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(findMachinePanelLayout.createSequentialGroup()
                        .add(findButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(findShowAllButton)))
                .addContainerGap())
        );
        findMachinePanelLayout.setVerticalGroup(
            findMachinePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(findMachinePanelLayout.createSequentialGroup()
                .add(findDescriptionLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(findMachinePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(findNameLabel)
                    .add(findNameTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(findMachinePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(availableAllRB)
                    .add(availableHomeRB)
                    .add(availableOutRB))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(findMachinePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(findButton)
                    .add(findShowAllButton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Søg", findMachinePanel);

        getDescriptionLabel.setText("Her kan der søges på maskiners ID");

        getButton.setText("Hent");
        getButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                getButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout getMachinePanelLayout = new org.jdesktop.layout.GroupLayout(getMachinePanel);
        getMachinePanel.setLayout(getMachinePanelLayout);
        getMachinePanelLayout.setHorizontalGroup(
            getMachinePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(getMachinePanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(getMachinePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(getDescriptionLabel)
                    .add(getButton)
                    .add(getIdSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 64, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(120, Short.MAX_VALUE))
        );
        getMachinePanelLayout.setVerticalGroup(
            getMachinePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(getMachinePanelLayout.createSequentialGroup()
                .add(getDescriptionLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(getIdSP, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(getButton)
                .addContainerGap(194, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Hent", getMachinePanel);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jTabbedPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 381, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jTabbedPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 328, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void addButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_addButtonActionPerformed
	{//GEN-HEADEREND:event_addButtonActionPerformed
		if(!addNameTF.getText().equals(""))
		{
			this.controller.addMachine(addNameTF.getText());
			JOptionPane.showMessageDialog(this, "Maskinen '"+addNameTF.getText()+"' er oprettet.", "Maskinen er oprettet", JOptionPane.INFORMATION_MESSAGE);
		}
		else
		{
			JOptionPane.showMessageDialog(this, "Der er ikke angivet et navn i tekstfeltet.", "Manglende navn", JOptionPane.WARNING_MESSAGE);
		}
	}//GEN-LAST:event_addButtonActionPerformed

	private void findButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_findButtonActionPerformed
	{//GEN-HEADEREND:event_findButtonActionPerformed
		boolean available;
		if(availableHomeRB.isSelected())
		{ available = true; }
		else
		{ available = false; }
		this.findTable.setModel(
			new MachineTableModel(
				this.controller.findMachine(findNameTF.getText(), available)));
	}//GEN-LAST:event_findButtonActionPerformed

	private void getButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_getButtonActionPerformed
	{//GEN-HEADEREND:event_getButtonActionPerformed
		Machine machine = this.controller.getMachine(Integer.parseInt(getIdSP.getValue().toString()));
		if (machine != null) {
			new MachineInfoView(machine).setVisible(true);
		} else {
			JOptionPane.showMessageDialog(this, "Der er angivet et forkert ID.", "Forkert ID", JOptionPane.ERROR_MESSAGE);
		}
	}//GEN-LAST:event_getButtonActionPerformed

	private void findShowAllButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_findShowAllButtonActionPerformed
	{//GEN-HEADEREND:event_findShowAllButtonActionPerformed
		findTable.setModel(new MachineTableModel(this.controller.getAllMachines()));
	}//GEN-LAST:event_findShowAllButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JLabel addDescriptionLabel;
    private javax.swing.JPanel addMachinePanel;
    private javax.swing.JLabel addNameLabel;
    private javax.swing.JTextField addNameTF;
    private javax.swing.JRadioButton availableAllRB;
    private javax.swing.ButtonGroup availableBG;
    private javax.swing.JRadioButton availableHomeRB;
    private javax.swing.JRadioButton availableOutRB;
    private javax.swing.JButton findButton;
    private javax.swing.JLabel findDescriptionLabel;
    private javax.swing.JPanel findMachinePanel;
    private javax.swing.JLabel findNameLabel;
    private javax.swing.JTextField findNameTF;
    private javax.swing.JButton findShowAllButton;
    private javax.swing.JTable findTable;
    private javax.swing.JButton getButton;
    private javax.swing.JLabel getDescriptionLabel;
    private javax.swing.JSpinner getIdSP;
    private javax.swing.JPanel getMachinePanel;
    private javax.swing.JScrollPane jScrollPane;
    private javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration//GEN-END:variables

	public void windowOpened(WindowEvent e)
	{}

	public void windowClosing(WindowEvent e)
	{
	}

	public void windowClosed(WindowEvent e)
	{}

	public void windowIconified(WindowEvent e)
	{}

	public void windowDeiconified(WindowEvent e)
	{}

	public void windowActivated(WindowEvent e)
	{}

	public void windowDeactivated(WindowEvent e)
	{}
}